<?php

/**
 * This is the model class for table "{{groups_discussion}}".
 *
 * The followings are the available columns in table '{{groups_discussion}}':
 * @property integer $id
 * @property string $name
 * @property integer $group_id
 * @property string $crdate
 */
class GroupsDiscussion extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return GroupsDiscussion the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{groups_discussion}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, text', 'required'),
			array('group_id, user_id, last_user, crdate, update', 'safe'),
			array('group_id', 'numerical', 'integerOnly'=>true),
			array('name', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, group_id, crdate', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user'=>array(self::BELONGS_TO, 'Users', 'user_id'),
			'lastUser'=>	array(self::BELONGS_TO,'Users','last_user'),
			'group'=>array(self::BELONGS_TO, 'Groups', 'group_id'),
			// GroupsDiscussionComments
			'countComments' 	=> 	array(self::STAT, 'GroupsDiscussionComments', 'discussion_id',
				// 'condition'	=>	'ruler=:ruler',
				// 'params' 	=> 	array(':ruler'=>true),
			),
			'comments' 			=> 	array(self::HAS_MANY, 'GroupsDiscussionComments', 'discussion_id',
				// 'condition'	=>	'ruler=:ruler',
				// 'params' 	=> 	array(':ruler'=>true),
				//'order' => 'id',
			),
			
			'commentsUsers'		=>	array(self::HAS_MANY,'Users',array('user_id'=>'id'),
				'through'	=>	'comments',
				// 'condition'	=>	'ruler=:ruler',
				// 'params' 	=> 	array(':ruler'=>true),
				// 'order' 	=> 	'contacts.id',
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Заголовок',
			'text' => 'Текст',
			'group_id' => 'Group',
			'crdate' => 'Crdate',
		);
	}

	/**
	 * This is invoked before the record is saved.
	 * @return boolean whether the record should be saved.
	 */
	protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord)
			{
				$this->crdate	=	$this->update 	= 	date('Y-m-d H:i:s');
				$this->group_id	=	$_GET['group'];
				$this->user_id	=	$this->last_user= 	Yii::app()->user->id;
			}
			else
			{
				$this->update 	= 	date('Y-m-d H:i:s');
				$this->last_user= 	Yii::app()->user->id;
			}
			return true;
		}
		else
			return false;
	}

	protected function beforeDelete()
	{
		if(parent::beforeDelete())
		{
			if($this->countComments != 0)
			{
				GroupsDiscussionComments::model()->deleteAll(
					'discussion_id=:discussion_id',
					array(':discussion_id'=>$this->id)
				);
				GroupsDiscussionCommentsLikes::model()->deleteAll(
					'discussion_comment_id=:discussion_comment_id',
					array(':discussion_comment_id'=>$this->id)
				);

			}
			
			return true;
		}
		else
			return false;
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('text',$this->name,true);
		$criteria->compare('group_id',$this->group_id);
		$criteria->compare('crdate',$this->crdate,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}